﻿Imports BUS
Imports DTO

Public Class frmPhieuThuTienPhat : Inherits DevComponents.DotNetBar.Office2007Form
    Private m_Nhanvien As CNhanVienDTO
    Public Property NhanVien() As CNhanVienDTO
        Get
            Return m_Nhanvien
        End Get
        Set(ByVal value As CNhanVienDTO)
            m_Nhanvien = value
        End Set
    End Property

    Public Function KiemTra() As Boolean
        Dim n As Integer
        If (Not Integer.TryParse(txtMaDocGia.Text, n)) Then
            MessageBox.Show("Mã độc giả không đúng", "Thông báo")
            Return False
        End If
        Dim dgia As CDocGiaDTO = New CDocGiaBUS().LayThongTinDocGiaKhiBietMa(n)
        If dgia Is Nothing Then
            MessageBox.Show("Không tồn tại độc giả này.")
            Return False
        End If
        txtHoTenDocGia.Text = dgia.HoTen
        txtTienNo.Text = dgia.TongNo
        Return True
    End Function
    Private Sub btnThongtin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThongtin.Click
        KiemTra()
    End Sub
    Public Sub Reset()
        txtMaDocGia.Text = ""
        txtHoTenDocGia.Text = ""
        txtTienNo.Text = ""
        txtSoTienThu.Text = ""
        txtConLai.Text = ""
    End Sub


    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Reset()
    End Sub

    Private Sub frmPhieuThuTienPhat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtMaNhanVien.Text = m_Nhanvien.Ma
        txtTenNhanVien.Text = m_Nhanvien.HoTen
    End Sub

    Private Sub btnThu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThu.Click
        If (Not KiemTra()) Then
            Return
        End If

        If Not KiemTraSoTienThu() Then
            Return
        End If

        Dim pt As New CPhieuThuTienPhatDTO()
        pt.MaDocGia = txtMaDocGia.Text
        pt.MaNhanVien = txtMaNhanVien.Text
        pt.SoTienThu = txtSoTienThu.Text
        pt.TienNo = txtTienNo.Text
        pt.ConLai = txtConLai.Text

        Dim kt As Boolean = New CPhieuThuTienPhatBUS().ThemPhieuThu(pt)
        MessageBox.Show("Thêm phiếu thu thành công.")
    End Sub

    Public Function KiemTraSoTienThu() As Boolean
        Dim sotienthu As Integer
        If txtSoTienThu.Text = "" Then
            MessageBox.Show("Số tiền thu không hợp lệ")
            Return False
        End If

        If (Not Integer.TryParse(txtSoTienThu.Text, sotienthu)) Then
            MessageBox.Show("Số tiền thu không hợp lệ")
            Return False
        End If
        If sotienthu = 0 Then
            MessageBox.Show("Không cần thực hiện phiếu thu")
            Return False
        End If
        Dim sotienno As Integer = Convert.ToInt32(txtTienNo.Text)
        Dim conlai As Integer = sotienno - sotienthu
        If conlai < 0 Then
            MessageBox.Show("Tiền thu không được vượt quá tiền nợ")
            txtSoTienThu.Focus()
            Return False
        End If
        txtConLai.Text = conlai.ToString()
        Return True
    End Function
    Private Sub txtSoTienThu_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSoTienThu.Leave
        If Not KiemTraSoTienThu() Then
            Return
        End If
    End Sub
End Class